﻿using AntdUI;

namespace DataAcquisition
{
    public partial class SelectTemplateForm : BaseForm
    {
        #region 自定义方法
        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadList()
        {
            List<Template> list = SqlHelper.Inquireable<Template>(null);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<Template> list)
        {
            var antItemList = new List<TemplateItem>();
            foreach (Template item in list)
            {
                string first = string.Empty;
                if (item.FirstStagePressure > 0 && item.FirstStageTime > 0)
                {
                    string timeUnit = item.FirstStageTimeUnit;
                    first = string.Format("压力:{0}Mpa--时长：{1}{2}", item.FirstStagePressure.ToString(), item.FirstStageTime.ToString(), timeUnit);
                }

                string second = string.Empty;
                if (item.SecondStagePressure > 0 && item.SecondStageTime > 0)
                {
                    string timeUnit = item.SecondStageTimeUnit;
                    second = string.Format("压力:{0}Mpa--时长：{1}{2}", item.SecondStagePressure.ToString(), item.SecondStageTime.ToString(), timeUnit);
                }

                string third = string.Empty;
                if (item.ThirdStagePressure > 0 && item.ThirdStageTime > 0)
                {
                    string timeUnit = item.ThirdStageTimeUnit;
                    third = string.Format("压力:{0}Mpa--时长：{1}{2}", item.ThirdStagePressure.ToString(), item.ThirdStageTime.ToString(), timeUnit);
                }

                string intensity = string.Empty;
                if (item.IntensityStagePressure > 0 && item.IntensityStageTime > 0)
                {
                    string timeUnit = item.IntensityStageTimeUnit;
                    intensity = string.Format("压力:{0}Mpa--时长：{1}{2}", item.IntensityStagePressure.ToString(), item.IntensityStageTime.ToString(), timeUnit);
                }

                string rigor = string.Empty;
                if (item.RigorStagePressure > 0 && item.RigorStageTime > 0)
                {
                    string timeUnit = item.RigorStageTimeUnit;
                    rigor = string.Format("压力:{0}Mpa--时长：{1}h", item.RigorStagePressure.ToString(), item.RigorStageTime.ToString(), timeUnit);
                }

                string pipe_position = string.Empty;
                if (item.PipePosition is not null)
                {
                    pipe_position = item.PipePosition;
                }

                antItemList.Add(new TemplateItem(item.ID,item.Number, pipe_position, item.DesignPressure,
                    first, second, third, intensity, rigor));
            }
            Table1.DataSource = antItemList;
        }
        #endregion

        #region 系统方法
        public SelectTemplateForm()
        {
            InitializeComponent();
            FullScreen = true;
        }

        private void SelectTemplateForm_Load(object sender, EventArgs e)
        {
            Table1.Height = this.Height;
            Table1.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("Number","模板编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipePosition","管道位置",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("DesignPressure","设计压力",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("FirstStage","第一阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("SecondStage","第二阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("ThirdStage","第三阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("IntensityStage","强度阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("RigorStage","严密阶段",AntdUI.ColumnAlign.Center)
            };
            Table1.Spin("正在加载中...", (config) =>
            {
                LoadList();
            });
        }

        private void Table1_CellDoubleClick(object sender, TableClickEventArgs e)
        {
            if (this.Owner is not null)
            {
                if (e.Record is not null)
                {
                    TemplateItem item = (TemplateItem)e.Record;
                    AddProjectForm frm = (AddProjectForm)this.Owner;
                    frm.templateObj = item;
                    this.Close();
                }
            }
            else
            {
                this.Close();
            }
        }

        private void SelectTemplateForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.Owner is not null)
            {
                AddProjectForm frm = (AddProjectForm)this.Owner;
                frm.LoadTemplateInfo();
            }
        }

        #endregion
    }
}
